#!/bin/bash
 
source make_sandbox.cfg
 
IFS=$'\n';


echo '# step 5: update destination database to reflect new URL >>> BEGIN';
# ============================================================
# step 5: update destination database to reflect new URL
# ============================================================
 
echo "changing mu blogs tables: source site->destination site";
 
echo "update ${DESTINATION_DATABASE}.wp_blogs set domain='${DESTINATION_SITE}'" \
	| mysql -u${DESTINATION_DATABASE_USER} -p${DESTINATION_DATABASE_PASS};
echo "update ${DESTINATION_DATABASE}.wp_site set domain='${DESTINATION_SITE}'" \
	| mysql -u${DESTINATION_DATABASE_USER} -p${DESTINATION_DATABASE_PASS};
echo \
"update 
	${DESTINATION_DATABASE}.wp_sitemeta 
set 
	meta_value='http://${DESTINATION_SITE}' 
where 
	meta_key='siteurl'" \
	| mysql \
		-u${DESTINATION_DATABASE_USER} \
		-p${DESTINATION_DATABASE_PASS};
		
		
echo "changing mu options tables: source site->destination site and source path->destination path";
	
tables=`echo "show tables from ${DESTINATION_DATABASE}" \
	| mysql -u${DESTINATION_DATABASE_USER} \
	-p${DESTINATION_DATABASE_PASS} \
	| grep "_options"`
 
for table in $tables
do
	echo "    - changing ${table}: source site->destination site";

	echo \
"update 
	${DESTINATION_DATABASE}.${table} 
set 
	option_value=replace(
		option_value,
		'${SOURCE_SITE}',
		'${DESTINATION_SITE}'
	)
where option_value LIKE '%${SOURCE_SITE}%'" \
	| mysql \
		-u${DESTINATION_DATABASE_USER} \
		-p${DESTINATION_DATABASE_PASS};

	echo "    - changing ${table}: source path->destination path";

	echo \
"update 
	${DESTINATION_DATABASE}.${table} 
set 
	option_value=replace(
		option_value,
		'${SOURCE_FOLDER_NOENDINGSLASH}',
		'${DESTINATION_FOLDER_NOENDINGSLASH}'
	)
where option_value LIKE '%${SOURCE_FOLDER_NOENDINGSLASH}%'" \
	| mysql \
		-u${DESTINATION_DATABASE_USER} \
		-p${DESTINATION_DATABASE_PASS};


done

echo '# step 5: update destination database to reflect new URL >>> END';
